package com.ilovepostcard.utils;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import com.ilovepostcard.R;

public class ImageUtil {

    public static final int GALLERY = 1;
    public static final int CAMERA = 4;
    public static final int CLIPRESULT = 3;

    public static Uri getImageFromCamera(Activity context) {
        String status = Environment.getExternalStorageState();
        Uri uri = null;
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Utils.ensureDirExist();
            File file = new File(Constants.TEMP_CAMERA_IMAGE_PATH);
            if (file.exists()) file.delete();
            
            uri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
            		uri);
            context.startActivityForResult(intent, CAMERA);
        } else {
            Toast.makeText(context, context.getString(R.string.no_sdcard),
                    Toast.LENGTH_SHORT).show();
        }
        return uri;
    }

    public static void setImageFromGallery(Activity context) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                "image/*");
        context.startActivityForResult(intent, GALLERY);
    }
    
    public static void startPhotoZoom(Activity context, Uri uri) {  

        Intent intent = new Intent("com.android.camera.action.CROP");  
        intent.setDataAndType(uri, "image/*");  
        //�������crop=true�������ڿ�����Intent��������ʾ��VIEW�ɲü�  
        intent.putExtra("crop", "true");  
        // aspectX aspectY �ǿ�ߵı���  
        intent.putExtra("aspectX", 1);  
        intent.putExtra("aspectY", 1);  
        // outputX outputY �ǲü�ͼƬ���  
        intent.putExtra("outputX", 100);  
        intent.putExtra("outputY", 100);  
        intent.putExtra("return-data", true);  
        context.startActivityForResult(intent, CLIPRESULT);  
    }

}
